package org.szgb.console.zx.task.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.line.entity.SysLineLocationSystemEquipment;
import org.szgb.console.sys.line.mapper.SysLineLocationSystemEquipmentMapper;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;
import org.szgb.console.zx.plan.entity.ZxPlan;
import org.szgb.console.zx.task.entity.ZxTask;
import org.szgb.console.zx.task.entity.ZxTaskForm;
import org.szgb.console.zx.task.mapper.ZxTaskFormMapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ZxTaskFormService {
    private final ZxTaskFormMapper zxTaskFormMapper;

    @Autowired
    public ZxTaskFormService(ZxTaskFormMapper zxTaskFormMapper) {
        this.zxTaskFormMapper = zxTaskFormMapper;
    }

    @Transactional(readOnly = true)
    public ZxTaskForm get(String id) {
        return zxTaskFormMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<ZxTaskForm> findList(ZxTaskForm zxTaskForm) {
        if (zxTaskForm != null && zxTaskForm.getPageDto() != null) {
            Page<ZxTaskForm> page = new Page<>(zxTaskForm.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            zxTaskForm.setPage(page);
        }
        return zxTaskFormMapper.findList(zxTaskForm);
    }

    @Transactional(readOnly = true)
    public Integer getCount(ZxTaskForm zxTaskForm) {
        return zxTaskFormMapper.getCount(zxTaskForm);
    }

    @Transactional()
    public void save(ZxTaskForm zxTaskForm) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (zxTaskForm.getId() == null || zxTaskForm.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            zxTaskForm.setCreatedBy(zxTaskForm.getUpdatedBy());
            // 添加UUID
            zxTaskForm.setId(UUIDUtil.getUUID());
            // 执行插入操作
            zxTaskFormMapper.insert(zxTaskForm);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            zxTaskFormMapper.update(zxTaskForm);
        }
    }

    @Transactional
    public void delete(String ids) {
        zxTaskFormMapper.delete(ids.split(","));
    }
}
